package com.mylib.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.mylib.libcore.R;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/20-9:38
 * @description CommonSwRecycleViewLayout-适用于简单布局
 **/
public class CustomBannerLayout extends LinearLayout  implements ViewPager.OnPageChangeListener{
    private Context mContext;
    private View mView;
    private ViewPager viewPager;
    private TextView title;
    private int  angle=0;
    private LinearLayout dotGroup;
    private List<String> imgUrlList = new ArrayList<>();
    private List<ImageView> imageList = new ArrayList<>();
    private List<String> titleTextList = new ArrayList<>();

    private boolean isSwitchPager = false; //默认不切换
    private int previousPosition = 0; //默认为0

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
        }
    };

    public CustomBannerLayout(Context context) {
        this(context, null);
    }

    public CustomBannerLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomBannerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    //定义一个接口对象listerner
    private IvBackClickListener listener;
    //获得接口对象的方法。
    public void setOnIvBackClickListener(IvBackClickListener listener) {
        this.listener = listener;
    }


    //定义一个接口
    public interface  IvBackClickListener{
        public void onClck();
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.bind_custom_banner, this, true);
        viewPager = (ViewPager) mView.findViewById(R.id.viewPager);
        dotGroup = (LinearLayout)mView.findViewById(R.id.dot_group);
        title = (TextView) mView.findViewById(R.id.title);
    }

//    //添加图片list
//    public void setImgUrlList(List imgUrlList){
//        if(!UIUtils.isEmpty(imgUrlList)){
//            this.imgUrlList =imgUrlList;
//        }
//    }
    public void hidenPoint(){
        dotGroup.setVisibility(GONE);
    }

    public void initViewPagerData(Context context,List imgUrlLists,List titleTextLists,int angles,int pointdDawable,int pointdSize,int pointMargin,int sleepTime) {
        if(!UIUtils.isEmpty(angles)){
            this.angle = angles;
        }
        if(!UIUtils.isEmpty(imgUrlLists)){
            this.imgUrlList =imgUrlLists;
        }else {
            return;
        }
        if(!UIUtils.isEmpty(titleTextLists)){
            this.titleTextList =titleTextLists;
        }

        ImageView imageView;
        View dotView;
        for (int i = 0; i < imgUrlList.size(); i++) {
            imageView = new ImageView(context);
            GlideUtil.loadFilletPic(context,imgUrlList.get(i),imageView,angle);
            imageList.add(imageView);
            //准备小圆点数据
            dotView = new View(context);
            if(UIUtils.isEmpty(pointdDawable)){
                dotView.setBackgroundResource(R.drawable.button_line_circle_ball);
            }else {
                dotView.setBackgroundResource(pointdDawable);
            }
            LinearLayout.LayoutParams params;
            //设置小圆点宽和高
            if(UIUtils.isEmpty(pointdSize)){
                params = new LinearLayout.LayoutParams(15, 15);
            }else {
                params = new LinearLayout.LayoutParams(pointdSize, pointdSize);
            }
            //设置每个小圆点之间的距离
            if(UIUtils.isEmpty(pointMargin)){
                if (i != 0) {
                    params.leftMargin = 15;
                }
            }else {
                if (i != 0) {
                    params.leftMargin = pointMargin;
                }
            }
            dotView.setLayoutParams(params);
            //设置小圆点状态
            dotView.setEnabled(false);
            //把dotView加入到线性布局中
            dotGroup.addView(dotView);
        }


        //设置当前viewPager要显示的第几个条目
        int item = Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % imageList.size());
        viewPager.setCurrentItem(item);
        //把第一个小圆点设置成白色，显示第一个TExtView内容
        dotGroup.getChildAt(previousPosition).setEnabled(true);
        if(!UIUtils.isEmpty(titleTextList) && previousPosition<titleTextList.size() ){
            title.setText(titleTextList.get(previousPosition));
        }
        viewPager.setAdapter(new MyViewPager());
        //设置viewPager滑动监听事件
        viewPager.addOnPageChangeListener(this);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!isSwitchPager) {
                    if(UIUtils.isEmpty(sleepTime) || sleepTime<500){
                        SystemClock.sleep(3000);
                    }else {
                        SystemClock.sleep(sleepTime);
                    }
                    handler.sendEmptyMessage(0);
                }
            }
        }).start();

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }
    @Override
    public void onPageSelected(int position) {
        if(!UIUtils.isEmpty(imageList)){
            int newPostion = position % imageList.size();
            //取出postion的位置小圆点设置为true
            dotGroup.getChildAt(newPostion).setEnabled(true);
            //把一个小圆点设置为false
            dotGroup.getChildAt(previousPosition).setEnabled(false);
            if(!UIUtils.isEmpty(titleTextList) && previousPosition<titleTextList.size() ){
                title.setText(titleTextList.get(previousPosition));
            }
            previousPosition = newPostion;
        }
    }
    @Override
    public void onPageScrollStateChanged(int state) {
    }

    private class MyViewPager extends PagerAdapter {
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }
        //初始化每个条目要显示的内容
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
              int newPostion = position % imageList.size();
            //获取到条目要显示的内容imageView
            ImageView img = imageList.get(newPostion);
            container.addView(img);
            return img;
        }
        //是否复用当前view
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
        //销毁条目
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }


    public void setBannerDestroy() {
        isSwitchPager = false;
    }

}
